import Clipboard from 'clipboard'

export const copy = (text, event) => {
  const clipboard = new Clipboard(event.target, { text: () => text })

  return new Promise((resolve, reject) => {
    clipboard.on('success', () => {
      resolve()
      clipboard.destroy()
    })
    clipboard.on('error', () => {
      reject(new Error('Copy failed: ', text))
      clipboard.destroy()
    })
    clipboard.onClick(event)
  })
}
